
package Servlets;

import BeanManager.UserManager;
import Beans.PrivilegeType;
import Beans.User;
import Email.GMailManager;
import Global.Methods;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Raed Shomali
 */
public class RegisterServlet extends HttpServlet 
{   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html;charset=UTF-8");
        
        PrintWriter out = response.getWriter();
        
        try 
        {
            // Get Email ..
            String email = request.getParameter( "email" ) ;
            
            // Validate It ..
            if( Methods.isValidEmailAddress( email ) )
            {
                // Get Rest Of Information ...
                String firstName = request.getParameter( "firstName" ) ;
                String lastName = request.getParameter( "lastName" ) ;
                String phone = request.getParameter( "phone" ) ;
                String website = request.getParameter( "website" ) ;
                String password = request.getParameter( "password" ) ;
                String confirm = request.getParameter( "confirm" ) ;
                
                long phoneNumber = 0 ;
                
                // Validate that those Variables Contain Values ...
                if( firstName == null || lastName == null || firstName.trim().isEmpty() || lastName.trim().isEmpty() )
                {
                    out.println( "<h2 style = 'color:red' > You Must Enter Your First & Last Name </h2>" );
                }
                else
                {
                    // Validate
                    if( phone == null )
                    {
                        phone = "" ;
                        
                        phoneNumber = 0 ;
                    }
                    else
                    {
                        phoneNumber = Long.parseLong( phone ) ;
                    }
                    
                    // Validate
                    if( website == null )
                    {
                        website = "" ;
                    }
                    
                    // Validate Passwords ...
                    if( password == null || confirm == null || password.isEmpty() || confirm.isEmpty() || !password.equals( confirm ) )
                    {
                        out.println( "<h2 style = 'color:red' > Password and its Confirmation do Not match </h2>" );
                    }
                    else
                    {
                        // For Better Security .. Do a Double Encryption
                        String encryptedPassword = UserManager.encrypt( UserManager.encrypt( password ) ) ;
                        
                        // Create a New User ...
                        User user = new User() ;
                        
                        // Set Respective Information
                        user.setEmailAddress( email ) ;
                        user.setFirstName( firstName ) ;
                        user.setLastName( lastName ) ;
                        user.setIsConfirmed( false ) ;
                        user.setPassword( encryptedPassword ) ;
                        user.setPhoneNumber( phoneNumber ) ;
                        user.setPrivilegeType( PrivilegeType.JUG_MEMBER ) ;
                        user.setRegisteredDate( new Date() ) ;
                        user.setWebsiteURL( website ) ;
                        
                        // Was it Successful ?
                        if( UserManager.insertUser( user ) )
                        {
                            out.println( "<h2> User Registered Successfully (^^) </h2>" );
                            
                            // Send Email
                            GMailManager.sendMail( "Congratulations" , "You are Successfully Registered !" , email );
                        }
                        else
                        {
                            out.println( "<h2  style = 'color:red' > User Could Not Be Registered. </h2>" );
                        }
                    }
                }
            }
            else
            {
                out.println( "<h2 color = 'red' > Invalid Email </h2>" );
            }
        } 
        finally 
        { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
